#!/bin/bash
echo "=== 开始清理 Kubernetes 集群 ==="

# 停止 kubelet
sudo systemctl stop kubelet

# 重置 kubeadm
sudo kubeadm reset -f


# 清理目录
sudo rm -rf /etc/kubernetes
sudo rm -rf /var/lib/etcd
sudo rm -rf /var/lib/kubelet
sudo rm -rf $HOME/.kube
sudo rm -rf /etc/cni

# 清理网络
sudo ip link delete cni0 2>/dev/null || true
sudo ip link delete flannel.1 2>/dev/null || true

# 清理 iptables
sudo iptables -F && sudo iptables -t nat -F && sudo iptables -t mangle -F && sudo iptables -X
sudo iptables -t nat -X && sudo iptables -t mangle -X

# 杀死残留进程
sudo pkill -f kube-apiserver
sudo pkill -f kube-controller-manager
sudo pkill -f kube-scheduler
sudo pkill -f etcd

# 重启服务
sudo systemctl daemon-reload
sudo systemctl restart containerd
sudo systemctl start kubelet

echo "=== 清理完成 ==="
